
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Deque;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

import controller.ACSystem;
import model.Auction;
import model.Bid;
import model.Item;
import model.NonProfit;
import model.WestPanelEnum;

import javax.swing.JTextField;

/**
 * The Item Input Pane
 * Unused warnings are suppressed because we want to save this 
 * data for future use rather than remove the unused variables
 * 
 * @author Bob Landowski
 *
 */
@SuppressWarnings({ "unused", "serial" })
public class ItemInputPane extends JPanel {

	/** The Item number */
	private int my_item_number;

	/** The quantity */
	private int my_quantity;

	/** The Auction */
	private Auction my_auction;

	/** The Auction number */
	private int my_auction_number;

	/** The auction number */
	private String my_auction_owner;

	/** The description */
	private String my_description;

	/** The photo */
	private String my_photo;

	/** The bids */
	private Deque<Bid> my_bids;

	/** The item */
	private Item my_item;

	/** The auction name label */
	private JLabel my_auctionName;

	/** The item number label*/
	private JLabel my_itemNumber;

	/** The item description */
	private JTextArea my_itemDescription;

	/** The ACcontroller */
	private ACSystem my_ACcontroller;

	/** The item name field */
	private JTextField itemNameField;

	/** The minstartingbid text field */
	private JTextField minstartingbid;

	/** The donor field */
	private JTextField donorField;

	/** The size field */
	private JTextField sizeField;

	/** The stroageField */
	private JTextField stroageField;

	/** The condition field */
	private JTextField conditionField;

	/** The  west panel listener */
	private WestPanelListener wpl;

	/** The  west panel observable */
	private WestPanelObservable wpo = new WestPanelObservable();

	/** The auction name */
	private String my_auction_name;

	/** The item name */
	private String my_item_name;

	/** The min starting mid*/
	private String my_minStartingBid;

	/** The quantity field */
	private JTextField my_quantity_field;

	/** The item input pane */
	public ItemInputPane(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		setLayout(null);
		setPreferredSize(new Dimension(560, 560));

		JLabel auctionCentralItemLabel = new JLabel("Auction Central Item Input");
		auctionCentralItemLabel.setBounds(147, 16, 223, 20);
		add(auctionCentralItemLabel);

		JLabel auctionNameLabel = new JLabel("Auction Name:");
		auctionNameLabel.setBounds(15, 96, 106, 20);
		add(auctionNameLabel);

		my_auctionName = new JLabel(my_auction_name);
		my_auctionName.setBounds(147, 96, 300, 20);
		add(my_auctionName);

		JLabel itemCommentsLabel = new JLabel("Description:");
		itemCommentsLabel.setBounds(15, 246, 106, 20);
		add(itemCommentsLabel);

		JLabel quantityLabel = new JLabel("Quantity:");
		quantityLabel.setBounds(378, 126, 69, 20);
		add(quantityLabel);

		my_itemDescription = new JTextArea();
		my_itemDescription.setLineWrap(true);
		my_itemDescription.setWrapStyleWord(true);
		my_itemDescription.setText(my_description);
		my_itemDescription.setBounds(105, 246, 342, 127);
		add(my_itemDescription);

		JLabel itemNameLabel = new JLabel("Item Name");
		itemNameLabel.setBounds(15, 126, 98, 20);
		add(itemNameLabel);

		itemNameField = new JTextField();
		itemNameField.setBounds(121, 126, 242, 20);
		add(itemNameField);
		itemNameField.setColumns(10);

		JLabel minStartingBidLabel = new JLabel("Min starting bid:");
		minStartingBidLabel.setBounds(15, 156, 124, 20);
		add(minStartingBidLabel);

		minstartingbid = new JTextField();
		minstartingbid.setBounds(147, 156, 98, 20);
		add(minstartingbid);
		minstartingbid.setColumns(10);

		JLabel lblDonor = new JLabel("Donor:");
		lblDonor.setBounds(289, 156, 69, 20);
		add(lblDonor);

		donorField = new JTextField();
		donorField.setBounds(358, 156, 146, 20);
		add(donorField);
		donorField.setColumns(10);

		JLabel sizeLabel = new JLabel("Size:");
		sizeLabel.setBounds(15, 186, 69, 20);
		add(sizeLabel);

		sizeField = new JTextField();
		sizeField.setBounds(83, 186, 98, 20);
		add(sizeField);
		sizeField.setColumns(10);

		JLabel storageButton = new JLabel("Storage:");
		storageButton.setBounds(255, 186, 69, 20);
		add(storageButton);

		stroageField = new JTextField();
		stroageField.setBounds(343, 186, 146, 20);
		add(stroageField);
		stroageField.setColumns(10);

		JLabel conditionLabel = new JLabel("Condition:");
		conditionLabel.setBounds(15, 216, 84, 20);
		add(conditionLabel);

		conditionField = new JTextField();
		conditionField.setBounds(121, 216, 146, 20);
		add(conditionField);
		conditionField.setColumns(10);

		JButton submitButton = new JButton("Submit");
		submitButton.setBounds(44, 471, 115, 29);
		submitButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				saveItem();
				//my_ACcontroller.setWestPanel(WestPanelEnum.CALENDAR);
				//my_ACcontroller.getMainFrame().westPanelState(new Object());

			}
		});
		add(submitButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setBounds(312, 471, 115, 29);
		cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				my_ACcontroller.setWestPanel(WestPanelEnum.CALENDAR);
				my_ACcontroller.getMainFrame().westPanelState(new Object());

			}
		});
		add(cancelButton);

		my_quantity_field = new JTextField();
		my_quantity_field.setBounds(462, 126, 69, 20);
		add(my_quantity_field);
		my_quantity_field.setColumns(10);
		my_ACcontroller = the_ACcontroller;
	}

	/** The save Item method */
	public void saveItem(){	
		my_item = new Item(my_auction,itemNameField.getText(),my_quantity_field.getText(),
				minstartingbid.getText(),donorField.getText(), sizeField.getText(),
				stroageField.getText(),conditionField.getText(),my_itemDescription.getText());
		my_auction.addItem(my_item);
		my_ACcontroller.setWestPanel(WestPanelEnum.ITEM);
		my_ACcontroller.getMainFrame().westPanelState(my_item);
	}
	
	/** The getObjectData method */
	public void getObjectData(Object the_object){

		//Do not display details of the Item (the_object) here because no item has yet been made
		my_auction = ((NonProfit)my_ACcontroller.getCurrentUser()).getCurrentAuction();
		my_auctionName.setText(my_auction.getName());

	}
	
	
	/** 
	 * WestPanelObservable class 
	 */
	private class WestPanelObservable extends Observable {

		/** 
		 * WestPanelObservable constructor 
		 */
		WestPanelObservable() {
			super();
		}

		void buttonClicked(Object o) {
			setChanged();
			notifyObservers(o);
		}
	}

	/** 
	 * WestPanelListener class 
	 */
	private class WestPanelListener implements MouseListener {

		/** The mouse clicked method */
		@Override
		public void mouseClicked(MouseEvent arg0) {
			//System.out.println("Clicked on button " + ((JButton) arg0.getComponent()).getText());
		}

		/** The mouse entered method */
		@Override
		public void mouseEntered(MouseEvent arg0) {


		}

		/** The mouse exited method */
		@Override
		public void mouseExited(MouseEvent arg0) {


		}

		/** The mouse pressed method */
		@Override
		public void mousePressed(MouseEvent arg0) {

			//System.out.println("Pressed on button " + ((JButton) arg0.getComponent()).getText());
		}

		/** The mouse relesed method */
		@Override
		public void mouseReleased(MouseEvent arg0) {

			//System.out.println("Released on button " + ((JButton) arg0.getComponent()).getText());
		}

	}
}